package com.soft.idc.common.service;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.model.IdcDeviceType;

public class IdcDeviceTypeService extends IdcDeviceType {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1756945463939846725L;

	/**
	 * 查询设备类型的子设备类型(包括其本身)
	 * @param orgId
	 * @return
	 */
	public static List<Record> querySubType(Integer typeId){
		//查询用户组织的所有子组织
		StringBuffer queryTypeSql = new StringBuffer();
		queryTypeSql.append("select t.type_id , t.name , t.parent_id from idc_device_type t where t.parent_id = ? or t.type_id = ?");
		queryTypeSql.append(" union ");
		queryTypeSql.append("select tt.type_id , tt.name , tt.parent_id from idc_device_type tt where tt.parent_id in ( ");
		queryTypeSql.append("select ttt.type_id from idc_device_type ttt where ttt.parent_id = ? ");
		queryTypeSql.append(") ");
		List<Record> orgList = Db.find(queryTypeSql.toString() , new Object[]{typeId, typeId, typeId});
		return orgList;		
	}
	
	public static String querySubTypeId(Integer typeId){
		List<Record> result = IdcDeviceTypeService.querySubType(typeId);
		StringBuffer typeIds = new StringBuffer();
		for(int i=0; i<result.size(); i++){
			if(i!=result.size()-1){
				typeIds.append(result.get(i).getInt("type_id") + ",");
			}else{
				typeIds.append(result.get(i).getInt("type_id"));
			}
		}
		return typeIds.toString();
	}
}
